package com.smec.remes.module.register.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.hwangjr.rxbus.Bus;
import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutRecyclerviewMantrBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.RollerDialog;

import java.util.ArrayList;
import java.util.HashMap;

import static com.smec.remes.module.register.activity.SmecMantrActivity.MANTR_BUY_NOT_NULL;

/**
 * Created by Administrator on 2017/12/9.
 */

public class SmecMantrAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {

    private ArrayList<AbleMatnr> arrayList=new ArrayList<>();
    private ArrayList<AbleMatnr> buycarList=new ArrayList<>();
    private Context mcontext;
    private Bus mRxbus;
    private RollerDialog rollerDialog;


    public SmecMantrAdapter(Context context,Bus smecRxBus) {
        this.mcontext=context;
        this.mRxbus=smecRxBus;
    }

    public void getList(ArrayList<AbleMatnr> list,String status){
        if(status.equals("onRefresh")){
            arrayList=list;
        }else {
            arrayList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void _setBuyList(ArrayList<AbleMatnr> list){
        if(CommonUtils.notEmpty(list)){
            buycarList.addAll(list);
        }
    }

    public void setBuycarList(ArrayList<AbleMatnr> buycarList) {
        if(CommonUtils.notEmpty(buycarList)){
            this.buycarList = buycarList;
        }else {
            this.buycarList=new ArrayList<>();
        }
    }

    public void clearList(){
        arrayList.clear();
        notifyDataSetChanged();
    }

    public ArrayList<AbleMatnr> getBuycarList(){
        return buycarList;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutRecyclerviewMantrBinding layoutRecyclerviewMantrBinding=DataBindingUtil.inflate
                (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_mantr,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(layoutRecyclerviewMantrBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewMantrBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        holder.setIsRecyclable(false);
        final LayoutRecyclerviewMantrBinding layoutRecyclerviewMantrBinding= (LayoutRecyclerviewMantrBinding) holder.getBinding();
        layoutRecyclerviewMantrBinding.setVariable(BR.mantr,arrayList.get(position));


        layoutRecyclerviewMantrBinding.ivItemadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rollerDialog=new RollerDialog(mcontext,R.style.project_back);
                rollerDialog.show();
                rollerDialog.reduction();
                TextView textView= (TextView) rollerDialog.findViewById(R.id.tv_roll_mantr);
                textView.setText(arrayList.get(position).getMatnr());
                Button button= (Button) rollerDialog.findViewById(R.id.btn_sure);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(rollerDialog.Num().equals("0")){
                            Toast.makeText(mcontext,"选取数量请大于零",Toast.LENGTH_SHORT).show();
                        }else {
//                            if(Integer.parseInt(rollerDialog.Num())>arrayList.get(position).getAvaliableQty()){
//                                Toast.makeText(mcontext,"输入数量不能大于分配数量",Toast.LENGTH_SHORT).show();
//                                return;
//                            }
                            arrayList.get(position).setNum(rollerDialog.Num());

                            int[] location = new int[2];
                            layoutRecyclerviewMantrBinding.ivItemadd.getLocationOnScreen(location);
                            _chechCar(arrayList.get(position),location);
                        }
                        rollerDialog.dismiss();
                    }
                });
            }
        });

        layoutRecyclerviewMantrBinding.llAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rollerDialog=new RollerDialog(mcontext,R.style.project_back);
                rollerDialog.show();
                rollerDialog.reduction();
                TextView textView= (TextView) rollerDialog.findViewById(R.id.tv_roll_mantr);
                textView.setText(arrayList.get(position).getMatnr());
                Button button= (Button) rollerDialog.findViewById(R.id.btn_sure);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(rollerDialog.Num().equals("0")){
                            Toast.makeText(mcontext,"选取数量请大于零",Toast.LENGTH_SHORT).show();
                        }else {
//                            if(Integer.parseInt(rollerDialog.Num())>arrayList.get(position).getAvaliableQty()){
//                                Toast.makeText(mcontext,"输入数量不能大于分配数量",Toast.LENGTH_SHORT).show();
//                                return;
//                            }
                            arrayList.get(position).setNum(rollerDialog.Num());

                            int[] location = new int[2];
                            layoutRecyclerviewMantrBinding.ivItemadd.getLocationOnScreen(location);
                            _chechCar(arrayList.get(position),location);
                        }
                        rollerDialog.dismiss();
                    }
                });
            }
        });
    }

    private void _chechCar(AbleMatnr ableMatnr,int[] location){
        boolean isHave=false;
        if(CommonUtils.notEmpty(buycarList)){
            for(int i=buycarList.size()-1;i>=0;i--){
                AbleMatnr matnr=buycarList.get(i);
                if(matnr.getMatnr()!=null && ableMatnr.getMatnr()!=null && matnr.getMatnr().equals(ableMatnr.getMatnr())){
                    isHave=true;
                    Toast.makeText(mcontext,"已添加至列表或请重新输入申请数量",Toast.LENGTH_SHORT).show();
                    buycarList.remove(i);
                    buycarList.add(ableMatnr);
                    break;
                }
            }
        }
        if(!isHave){
            buycarList.add(ableMatnr);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("size",new String(buycarList.size()+""));
        hashMap.put("x",location[0]);
        hashMap.put("y",location[1]);
        mRxbus.post(MANTR_BUY_NOT_NULL,hashMap);
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }
}
